﻿using Abp.Domain.Entities.Auditing;
using FutureApp.Users;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace FutureApp.Shop
{
    /// <summary>
    /// 库存事务
    /// </summary>
    [Table("GoodsStockTransactions")]
    public class GoodsStockTransaction : CreationAuditedEntity<Guid, User>
    {
        /// <summary>
        /// 商品Id
        /// </summary>
        public virtual Guid GoodsId { get; set; }

        /// <summary>
        /// 商品
        /// </summary>
        [ForeignKey(nameof(GoodsId))]
        public virtual Goods Goods { get; set; }

        /// <summary>
        /// 库存计算（1：库存入库，-1：库存出库，0：不计算库存）
        /// </summary>
        [Range(-1, 1)]
        public virtual int Direction { get; set; }

        /// <summary>
        /// 事务变更数量
        /// </summary>
        [Range(0, int.MaxValue)]
        public virtual int Quantity { get; set; }

        /// <summary>
        /// 事务数量
        /// </summary>
        [NotMapped]
        public int TransactionQuantity
        {
            get { return this.Direction * this.Quantity; }
            set
            {
                this.Quantity = Math.Abs(value);
                this.Direction = value / this.Quantity;
            }
        }
    }
}